﻿using ASmile.Com.GridView;
using ASmile.DBModel;
using ASmile.Extend;
using System.Linq;

namespace ASmile.Com.Da
{
    public class GridViewBLL : BLLCrudBase<ArgGetList, Sys_GridColumn>, IGridViewBLL
    {
        /// <summary>
        /// 根据form名称及dgv名称获取列名
        /// </summary>
        public ApiResultList<Sys_GridColumn> GetColumnInfo(ArgGetList arg)
        {
            var rData = DataCacheCom.GetGridColumn(s => s.FormName == arg.FormName);
            if (arg.DgvName.IsNotEmpty())
            {
                rData = rData?.FindAll(s => s.DgvName == arg.DgvName);
            }
            if (rData != null)
            {
                rData = rData.OrderBy(s => s.FormName).OrderBy(s => s.ColumnIndex).ToList();
            }
            return RetOK(rData, rData?.Count ?? 0, "OK");
        }

        protected override void SetQuery(ORM.Cores.QueryCondition<Sys_GridColumn> query, ArgGetList arg)
        {
            if (arg.FormName.IsNotEmpty())
            {
                query.WhereAnd(s => s.FormName == arg.FormName);
            }
            if (!arg.SearchVal.IsEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.FormName, likeStr) ||
                    s.WEx_Like(s.DgvName, likeStr) ||
                    s.WEx_Like(s.ColumnName, likeStr) ||
                    s.WEx_Like(s.ColumnTitle, likeStr)
                    );
            }
            query.OrderBy(s => s.FormName)
                 .OrderBy(s => s.DgvName)
                 .OrderBy(s => s.ColumnIndex);
        }

        protected override ApiResult<string> VerifyModel(Sys_GridColumn model, bool isAdd)
        {
            if (model.FormName.IsEmpty())
            {
                return RetErr(nameof(model.FormName), "窗体名称不能为空");
            }
            if (model.DgvName.IsEmpty())
            {
                return RetErr(nameof(model.DgvName), "Grid名称不能为空");
            }
            if (model.ColumnName.IsEmpty())
            {
                return RetErr(nameof(model.ColumnName), "列名不能为空");
            }
            if (model.ColumnTitle.IsEmpty())
            {
                return RetErr(nameof(model.ColumnTitle), "列标题不能为空");
            }
            if (model.ColumnIndex < 0)
            {
                return RetErr(nameof(model.ColumnIndex), "列索引必须大于0");
            }
            if (model.ColumnType.IsEmpty())
            {
                return RetErr(nameof(model.ColumnType), "列类型不能为空");
            }
            if (model.IsAdSearch)
            {
                if (model.DataType.IsEmpty())
                {
                    return RetErr(nameof(model.DataType), "搜索列可用时,必选选择数据类型");
                }
            }
            if (DB.QueryExist<Sys_GridColumn>(s =>
                    s.FormName == model.FormName &&
                    s.DgvName == model.DgvName &&
                    s.ColumnName == model.ColumnName &&
                    s.Id != model.Id))
            {
                return RetErr(nameof(model.ColumnIndex), "列名重复");
            }
            if (isAdd)
            {
                model.Id = NewId();
                return RetOK(model.Id, "新增成功");
            }
            else
            {
                return RetOK(string.Empty, "更新成功");
            }
        }

        public ApiResult SetColumnIndex(ArgSetSortNo arg)
        {
            Sys_GridColumn m = new Sys_GridColumn
            {
                Id = arg.Id,
                ColumnIndex = arg.SortNo
            };
            var r = DB.Update(m);
            if (r.RowCount == 1)
            {
                return RetOK("修改成功");
            }
            return RetErr($"修改失败");
        }

        public ApiResult SwapOrderNo(ArgSwapOrderNo arg)
        {
            if (arg.SrcId.IsEmpty() || arg.SrcId.IsEmpty())
            {
                return RetOK("参数错误");
            }
            Sys_GridColumn model = new Sys_GridColumn();
            model.Id = arg.SrcId;
            model.ColumnIndex = arg.TarOrderNo;
            DB.Update(model);
            model.Id = arg.TarId;
            model.ColumnIndex = arg.SrcOrderNo;
            DB.Update(model);
            return RetOK("交换位置成功");
        }
    }

    public class GridViewBLL_bak : BLLBase//, IGridViewBLL
    {
        /// <summary>
        /// 根据form名称及dgv名称获取列名
        /// </summary>
        public ApiResultList<Sys_GridColumn> GetColumnInfo(ArgGetList arg)
        {
            var rData = DataCacheCom.GetGridColumn(s => s.FormName == arg.FormName);
            if (arg.DgvName.IsNotEmpty())
            {
                rData = rData?.FindAll(s => s.DgvName == arg.DgvName);
            }
            if (rData != null)
            {
                rData = rData.OrderBy(s => s.FormName).OrderBy(s => s.ColumnIndex).ToList();
            }
            return RetOK(rData, rData?.Count ?? 0, "OK");
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        public ApiResultList<Sys_GridColumn> GetData(ArgPageData arg)
        {
            var query = DB.CreateQuery<Sys_GridColumn>();
            query.PageNo = arg.PageNo;
            query.PageSize = arg.PageSize;

            if (arg.TagData != null)
            {
                query.WhereAnd(s => s.FormName == arg.TagData.ToString());
            }

            if (!arg.SearchVal.IsEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.FormName, likeStr) ||
                    s.WEx_Like(s.DgvName, likeStr) ||
                    s.WEx_Like(s.ColumnName, likeStr) ||
                    s.WEx_Like(s.ColumnTitle, likeStr)
                    );
            }
            query.OrderBy(s => s.FormName)
                 .OrderBy(s => s.DgvName)
                 .OrderBy(s => s.ColumnIndex);

            var adSql = GetAdSearchSql(arg.AdSearchList);

            var ret = DB.Query<Sys_GridColumn>(query, adSql);
            var rList = ret.ToList();
            return RetOK(rList, query.Total);
        }

        /// <summary>
        /// 新增\修改 的model验证
        /// </summary>
        protected ApiResult<string> VerifyModel(Sys_GridColumn model, bool isAdd)
        {
            if (model.FormName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.FormName), "窗体名称不能为空");
            }
            if (model.DgvName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.DgvName), "Grid名称不能为空");
            }
            if (model.ColumnName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnName), "列名不能为空");
            }
            if (model.ColumnTitle.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnTitle), "列标题不能为空");
            }
            if (model.ColumnIndex < 0)
            {
                return ApiResult.RetErr(nameof(model.ColumnIndex), "列索引必须大于0");
            }
            if (model.ColumnType.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnType), "列类型不能为空");
            }
            if (model.IsAdSearch)
            {
                if (model.DataType.IsEmpty())
                {
                    return ApiResult.RetErr(nameof(model.DataType), "搜索列可用时,必选选择数据类型");
                }
            }
            if (DB.QueryExist<Sys_GridColumn>(s =>
                    s.FormName == model.FormName &&
                    s.DgvName == model.DgvName &&
                    s.ColumnName == model.ColumnName &&
                    s.Id != model.Id))
            {
                return ApiResult.RetErr(nameof(model.ColumnIndex), "列名重复");
            }
            if (isAdd)
            {
                model.Id = NewId();
                return RetOK(model.Id, "新增成功");
            }
            else
            {
                return RetOK(string.Empty, "更新成功");
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        public virtual ApiResult<string> Add(Sys_GridColumn model)
        {
            var ret = VerifyModel(model, true);
            if (ret)
            {
                DB.Insert(model);
            }
            return ret;
        }
        /// <summary>
        /// 更新
        /// </summary>
        public virtual ApiResult<string> Edit(Sys_GridColumn model)
        {
            var ret = VerifyModel(model, false);
            if (ret)
            {
                DB.Update(model);
            }
            return ret;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public ApiResult Del(Sys_GridColumn model)
        {
            var r = DB.Delete(model);
            //DB.Delete<Sys_GridColumn>(s =>
            //    s.FormName == model.FormName &&
            //    s.DgvName == model.DgvName &&
            //    s.ColumnIndex == model.ColumnIndex);
            if (r.RowCount == 1)
            {
                return ApiResult.RetOK("删除成功");
            }
            else
            {
                return ApiResult.RetErr("删除失败");
            }
        }
        /// <summary>
        /// 设置列的排序
        /// </summary>
        public ApiResult SetColumnIndex(ArgSetSortNo arg)
        {
            Sys_GridColumn m = new Sys_GridColumn
            {
                Id = arg.Id,
                ColumnIndex = arg.SortNo
            };
            var r = DB.Update(m);
            if (r.RowCount == 1)
            {
                return RetOK("修改成功");
            }
            return RetErr($"修改失败");
        }
        /// <summary>
        /// 交换两个行的排序
        /// </summary>
        public ApiResult SwapOrderNo(ArgSwapOrderNo arg)
        {
            if (arg.SrcId.IsEmpty() || arg.SrcId.IsEmpty())
            {
                return RetOK("参数错误");
            }
            Sys_GridColumn model = new Sys_GridColumn();
            model.Id = arg.SrcId;
            model.ColumnIndex = arg.TarOrderNo;
            DB.Update(model);
            model.Id = arg.TarId;
            model.ColumnIndex = arg.SrcOrderNo;
            DB.Update(model);
            return RetOK("交换位置成功");
        }
    }
}